package fr.labri.macchiato.client.cart;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;

import fr.labri.macchiato.client.cart.events.ValidateSearchHandler;
import fr.labri.macchiato.framework.business.model.businessEntity.BusinessEntity;
import fr.labri.macchiato.framework.business.model.preferences.UserPreferences;
import fr.labri.macchiato.framework.business.finder.Callback;
import fr.labri.macchiato.framework.business.model.product.Offer;
import fr.labri.macchiato.framework.business.service.Services;

/**
 * A widget searching for available offers for a specified model
 * 
 * @author Paul SARRAUTE
 *
 */
public class OfferSearch extends ProductSearch {

	private RadioGroupItem offerItems;

	private List<Offer> resultOffers;

	/**
	 * Create the widget searching available offers
	 * 
	 * @param modelEan The EAN barcode of the model to search offer for
	 */
	OfferSearch(String modelEan) {
		super(modelEan);

		resultOffers = null;
	}

	private void addOffers(List<Offer> offers) {
		resultOffers = offers;
		
		resultNumber.setContents(resultOffers.size() + " élément(s) trouvé(s)");

		offerItems = new RadioGroupItem();
		offerItems.setRequired(true);
		offerItems.setTitle("Choisissez un fournisseur: ");

		List<String> offersDescription = new ArrayList<String>();
		for (Offer offer : offers) {
			offersDescription.add(offer.getProvider().getLegalName() + ": "
					+ NumberFormat.getCurrencyFormat().format(offer.getPrice()));
		}

		offerItems.setValueMap(offersDescription.toArray(new String[0]));

		contents.setFields(offerItems);
		displayContents();
	}

	@Override
	void addValidateHandler(final ValidateSearchHandler handler) {
		validButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Offer selectedOffer = getSelectedOffer();

				handler.setSelectedOffer(selectedOffer);
				handler.fireEvent();

			}

		});
	}

	private Offer getSelectedOffer() {
		Offer offer = null;

		String selected = offerItems.getValueAsString();

		for (Offer o : resultOffers) {
			if (selected.contains(o.getProvider().getLegalName())) {
				offer = o;
				break;
			}
		}

		return offer;
	}

	@Override
	void startSearch() {
		if (resultOffers == null) {
			progressbar = new ProgressLabel("Recherche en cours...");
			addMember(progressbar);
			
			List<BusinessEntity> favoriteBusinessEntity = getUserPreferences().getFavoriteBusinessEntities();
			
			getProductFinder().getOffersByEan(searchKeyword, favoriteBusinessEntity,
					new Callback<List<Offer>>() {

						@Override
						public void onCallback(List<Offer> result) {
							addOffers(result);
						}
					});
		} else {
			show();
		}

	}
	
	/**
	 * 
	 * @return The user's preferences
	 */
	UserPreferences getUserPreferences() {
		return Services.getInstance().getPreferencesService().getUserPreferences();
	}

}
